/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Windows.Forms;
using SurveillanceDotNet.Model;

namespace SurveillanceDotNet
{
    public partial class SystemConfigurationForm : Form
    {
        public SystemConfigurationForm()
        {
            InitializeComponent();
        }

        private void SystemConfigurationForm_Load(object sender, EventArgs e)
        {
            tbxSystemStartTimeout.Text = (Database.Instance.SystemStartTimeout / 1000).ToString();
            tbxAlarmInactivityTimeout.Text = (Database.Instance.AlarmTimeOutPeriod / 1000).ToString();

            if (Database.Instance.MailAccounts.Count == 0)
            {
                gbRemoteEMailControl.Enabled = false;
            }
            else
            {
                chxEnableRemoteEMailControl.Checked = Database.Instance.EnableRemoteEMailControl;

                foreach (MailAccount account in Database.Instance.MailAccounts)
                {
                    cbxUsedEMailAccount.Items.Add(account);
                }

                cbxUsedEMailAccount.SelectedItem = Database.Instance.RemoteEMailControl.MailAccount;

                tbxCheckMailInterval.Text = (Database.Instance.RemoteEMailControl.UpdateTimerInterval / 60000).ToString();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int systemStartTimeout;

            if (!int.TryParse(tbxSystemStartTimeout.Text, out systemStartTimeout))
            {
                MessageBox.Show("The system start timeout value is not a valid number!", "Invalid system timeout value", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Database.Instance.SystemStartTimeout = systemStartTimeout * 1000;

            int alarmTimeout;

            if (!int.TryParse(tbxAlarmInactivityTimeout.Text, out alarmTimeout))
            {
                MessageBox.Show("The alarm timeout value is not a valid number!", "Invalid alarm timeout value", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Database.Instance.AlarmTimeOutPeriod = alarmTimeout * 1000;

            if (Database.Instance.MailAccounts.Count != 0)
            {

                Database.Instance.EnableRemoteEMailControl = chxEnableRemoteEMailControl.Checked;

                if (chxEnableRemoteEMailControl.Checked && cbxUsedEMailAccount.SelectedIndex == -1)
                {
                    MessageBox.Show("Please select an EMail account!", "No EMail account selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Database.Instance.RemoteEMailControl.MailAccount = (MailAccount)cbxUsedEMailAccount.SelectedItem;

                int eMailCheckInterval;

                if (!int.TryParse(tbxCheckMailInterval.Text, out eMailCheckInterval))
                {
                    MessageBox.Show("The EMail check interval value is not a valid number!", "Invalid EMail check interval", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Database.Instance.RemoteEMailControl.UpdateTimerInterval = eMailCheckInterval * 60000;
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
